﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace igflow.GameObject
{
    public class igGameObject
    {
        public GameObject.igGameObject parent;
        public List<GameObject.igGameObject> Children;
        public string name;
        Vector3 _localPosition;
        public Vector3 Position
        {
           get
            {
                if (parent != null)
                {
                    return parent.Position + _localPosition;
                }
                return _localPosition;
            }
            set {
                if (parent == null)
                {
                    _localPosition = value;
                }
                else
                {
                    _localPosition = value - parent.Position;
                }
            }
        }

        Quaternion _localRotation;
        public Quaternion Rotation
        {
            get { return _localRotation; }
            set { _localRotation = value; }
        }

        // Gets the matrix representing this object in 3D space
        public Matrix WorldMatrix
        {
            get 
            {
               return Matrix.CreateFromQuaternion(Rotation) * Matrix.CreateTranslation(Position);
            }
        }

        internal List<Components.igComponent> _components;

        public igGameObject() : this (""){}
        public igGameObject(string n)
        {
            name = n;
            _localPosition = Vector3.Zero;
            _localRotation = Quaternion.Identity;
        }

        public void AddComponent(Components.igComponent component)
        {
            if (_components == null)
            {
                _components = new List<igflow.Components.igComponent>();
            }
            component.gameObject = this; // Set the us to be the components gameObject
            component.Init();
            _components.Add(component);
        }

        public void SendMessage(string message)
        {
            SendMessage(message, null);
        }

        public void SendMessage(string message, object[] args)
        {
            if (_components != null)
            {
                foreach (Components.igComponent component in _components)
                {
                    if (component.GetType().GetMethod(message) != null)
                    {
                        component.GetType().GetMethod(message).Invoke(component, args);
                    }
                }
            }
        }

        public void AddChild(GameObject.igGameObject child)
        {
            if (Children == null)
            {
                Children = new List<igGameObject>();
            }
            Vector3 p = child.Position;
            Children.Add(child);
            child.parent = this;
            // Reset the position
            child.Position = p;
        }
    }
}
